package com.hy.controller;

import com.hy.common.Result;
import com.hy.dto.regionDto.RegionTreeDto;
import com.hy.entity.City;
import com.hy.entity.District;
import com.hy.entity.Province;
import com.hy.service.CityService;
import com.hy.service.DistrictService;
import com.hy.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/regions")
public class RegionController {

    @Autowired
    private ProvinceService provinceService;
    
    @Autowired
    private CityService cityService;
    
    @Autowired
    private DistrictService districtService;

    /**
     * 获取地区树形结构
     */
    @GetMapping("/tree")
    public Result<List<RegionTreeDto>> getRegionTree() {
        return Result.success(provinceService.getRegionTree());
    }

    /**
     * 获取所有省份
     */
    @GetMapping("/provinces")
    public Result<List<Province>> getProvinces() {
        return Result.success(provinceService.getAllProvinces());
    }

    /**
     * 根据ID获取省份
     */
    @GetMapping("/provinces/{id}")
    public Result<Province> getProvince(@PathVariable Long id) {
        return Result.success(provinceService.getProvinceById(id));
    }

    /**
     * 添加省份
     */
    @PostMapping("/provinces")
    public Result<Boolean> addProvince(@RequestBody Province province) {
        return Result.success(provinceService.addProvince(province));
    }

    /**
     * 修改省份
     */
    @PutMapping("/provinces/{id}")
    public Result<Boolean> updateProvince(@PathVariable Long id, @RequestBody Province province) {
        province.setId(id);
        return Result.success(provinceService.updateProvince(province));
    }

    /**
     * 删除省份
     */
    @DeleteMapping("/provinces/{id}")
    public Result<Boolean> deleteProvince(@PathVariable Long id) {
        return Result.success(provinceService.deleteProvince(id));
    }

    /**
     * 获取所有城市
     */
    @GetMapping("/cities")
    public Result<List<City>> getCities() {
        return Result.success(cityService.getAllCities());
    }

    /**
     * 根据ID获取城市
     */
    @GetMapping("/cities/{id}")
    public Result<City> getCity(@PathVariable Long id) {
        return Result.success(cityService.getCityById(id));
    }

    /**
     * 添加城市
     */
    @PostMapping("/cities")
    public Result<Boolean> addCity(@RequestBody City city) {
        return Result.success(cityService.addCity(city));
    }

    /**
     * 修改城市
     */
    @PutMapping("/cities/{id}")
    public Result<Boolean> updateCity(@PathVariable Long id, @RequestBody City city) {
        city.setId(id);
        return Result.success(cityService.updateCity(city));
    }

    /**
     * 删除城市
     */
    @DeleteMapping("/cities/{id}")
    public Result<Boolean> deleteCity(@PathVariable Long id) {
        return Result.success(cityService.deleteCity(id));
    }

    /**
     * 获取所有区县
     */
    @GetMapping("/districts")
    public Result<List<District>> getDistricts() {
        return Result.success(districtService.getAllDistricts());
    }

    /**
     * 根据ID获取区县
     */
    @GetMapping("/districts/{id}")
    public Result<District> getDistrict(@PathVariable Long id) {
        return Result.success(districtService.getDistrictById(id));
    }

    /**
     * 添加区县
     */
    @PostMapping("/districts")
    public Result<Boolean> addDistrict(@RequestBody District district) {
        return Result.success(districtService.addDistrict(district));
    }

    /**
     * 修改区县
     */
    @PutMapping("/districts/{id}")
    public Result<Boolean> updateDistrict(@PathVariable Long id, @RequestBody District district) {
        district.setId(id);
        return Result.success(districtService.updateDistrict(district));
    }

    /**
     * 删除区县
     */
    @DeleteMapping("/districts/{id}")
    public Result<Boolean> deleteDistrict(@PathVariable Long id) {
        return Result.success(districtService.deleteDistrict(id));
    }
} 